package thread;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author yujiafan
 * @version 1.0.0
 * @create 2023-05-23
 */
public class CountDownLatchTest {

    public static void main(String[] args) throws Exception {
        ExecutorService executor = Executors.newFixedThreadPool(3);
        List<Integer> list = new ArrayList<>();
        for (int i = 1; i <= 10000; i++) {
            list.add(i);
        }
        CountDownLatch countDownLatch = new CountDownLatch(list.size());
        for (Integer integer : list) {
            Runnable runnable = new Runnable() {
                @Override
                public void run() {
                    try {
                        if(integer % 2 == 0){
                            System.out.println("---");
                            return;
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        System.out.println(integer);
                        countDownLatch.countDown();
                    }
                }
            };
            executor.submit(runnable);
        }
        countDownLatch.await();
        executor.shutdown();
        System.out.println("over!");

    }
}
